//
//  PCDApiSetStorage.m
//  PCDBank
//
//  Created by lyy on 2021/3/17.
//  Copyright © 2021 DK. All rights reserved.
//

#import "PCDApiSetStorage.h"
#import "PCDDataBaseModel.h"

@implementation PCDApiSetStorage

- (void)handler:(id)data andContext:(PCDContext *)context ResponseCallback:(WVJBResponseCallback)responseCallback {
    
    NSDictionary *parameters = (NSDictionary *)data;
    NSString *callBack = [parameters objectForKey:@"success"];

    
    NSString *keyString = [parameters objectForKey:@"key"];
    if (!keyString) {
        keyString = @"default";
    }
    id dataInfo = [parameters objectForKey:@"data"];
    if (!dataInfo) {
        dataInfo = @"";
    }
    //数据持久化 应用模式使用
    BOOL lasted = [[parameters objectForKey:@"lasted"] boolValue];
    if (lasted) {
        //需要持久化则删除内存中的数据
        [[PCDDataBaseModel shareModel].memoryStorage removeObjectForKey:keyString];
        [[PCDDataBaseModel shareModel] setStorageToDataBaseWith:parameters];
    } else {
        //不需要持久化则删除持久化的数据
        [[PCDDataBaseModel shareModel].memoryStorage setObject:dataInfo forKey:keyString];
        [[PCDDataBaseModel shareModel] removeStorageFromDataBaseWith:parameters];
    }
    
    evaluateJavaScriptOnMainThread_quotes(self, callBack, @"");
}

@end
